package com.gitee.fmadmin.service.sys;

import com.gitee.fmadmin.common.support.ServiceSupport;
import com.gitee.fmadmin.dao.entity.SysResource;
import com.gitee.fmadmin.dao.mapper.SysResourceMapper;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;


/**
 * @author 六如
 */
@Service
public class SysResourceService implements ServiceSupport<SysResource, SysResourceMapper> {

    public int deleteDeeply(Long id) {
        int cnt = this.deleteById(id);

        // 拿到子节点
        List<Long> childIds = this.query()
                .eq(SysResource::getParentId, id)
                .listUniqueValue(SysResource::getId);

        if (!childIds.isEmpty()) {
            for (Long childId : childIds) {
                cnt += this.deleteDeeply(childId);
            }
        }

        return cnt;
    }

    @Override
    public List<SysResource> listAll() {
        List<SysResource> list = ServiceSupport.super.listAll();
        list.sort(Comparator.comparing(SysResource::getRank));
        return list;
    }
}
